using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using TimeManager.Web.Models;
using TimeManager.Web.ViewModels;


namespace TimeManager.Web.Controllers {

    public class ReportsController : Controller {
        private readonly IUserRepository userRepository;
        private readonly ITaskRepository taskRepository;
        private readonly ICategoryRepository categoryRepository;
        private readonly IProjectRepository projectRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public ReportsController() : this(new UserRepository(), new TaskRepository(), new CategoryRepository(), new ProjectRepository()) { }

        public ReportsController(IUserRepository userRepository, ITaskRepository taskRepository, ICategoryRepository categoryRepository, IProjectRepository projectRepository) {
            this.userRepository = userRepository;
            this.taskRepository = taskRepository;
            this.categoryRepository = categoryRepository;
            this.projectRepository = projectRepository;
        }


        public ViewResult Index() {
            return View();
        }


        public ViewResult UserTotals() {
            var totals = new List<UserTotalsViewModel>();
            foreach (var user in userRepository.All) {
                var total = new UserTotalsViewModel();
                total.Name = user.Name;
                total.UserId = user.UserId;
                foreach (var task in taskRepository.All.Where(t => t.UserId == user.UserId)) {
                    total.TotalHours += task.Duration.TotalHours;
                }
                totals.Add(total);
            }

            return View(totals);
        }


        public ActionResult UserTotalsChart() {
            var viewModel = new ChartViewModel();

            var totals = new List<KeyValuePair<String, Double>>();
            foreach (var user in userRepository.All) {
                Double hours = 0;
                foreach (var task in taskRepository.All.Where(t => t.UserId == user.UserId)) {
                    hours += task.Duration.TotalHours;
                }
                var total = new KeyValuePair<String, Double>(user.Name, hours);
                totals.Add(total);
            }

            ViewBag.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie.ToString();
            viewModel.Title = "User Totals";
            viewModel.XValue = totals.ToList().Select(p => p.Key).ToArray();
            viewModel.YValues = totals.ToList().Select(p => p.Value).ToArray();

            return View("Chart", viewModel);
        }


        public ViewResult ProjectTotals() {
            var totals = new List<UserTotalsViewModel>();

            foreach (var proj in projectRepository.All) {
                var total = new UserTotalsViewModel();
                total.Name = proj.Name;
                total.UserId = proj.ProjectId;
                foreach (var task in taskRepository.All.Where(t => t.ProjectId == proj.ProjectId)) {
                    total.TotalHours += task.Duration.TotalHours;
                }
                totals.Add(total);
            }

            return View(totals);
        }


        public ActionResult ProjectTotalsChart() {
            var viewModel = new ChartViewModel();

            var totals = new List<KeyValuePair<String, Double>>();
            foreach (var proj in projectRepository.All) {
                Double hours = 0;
                foreach (var task in taskRepository.All.Where(t => t.ProjectId == proj.ProjectId)) {
                    hours += task.Duration.TotalHours;
                }
                var total = new KeyValuePair<String, Double>(proj.Name, hours);
                totals.Add(total);
            }

            ViewBag.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Bar.ToString();
            viewModel.Title = "Project Totals";
            viewModel.XValue = totals.ToList().Select(p => p.Key).ToArray();
            viewModel.YValues = totals.ToList().Select(p => p.Value).ToArray();

            return View("Chart", viewModel);
        }

    }
}

